package ch.zbw.pra.sysventory.domain.visualisation;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import ch.zbw.pra.sysventory.SysventoryApp;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator.FileType;
import ch.zbw.pra.sysventory.domain.model.Computer;
import ch.zbw.pra.sysventory.domain.model.OperatingSystem;
import ch.zbw.pra.sysventory.domain.model.Processor;

import com.thoughtworks.xstream.XStream;

public class VisuControll {
	private static VisuControll instance = null;

	public static VisuControll getInstance() {
		if (instance == null) {
			try {
				instance = (VisuControll) new XStream().fromXML(new FileGenerator().getFile(FileType.SETTING));
			} catch (Exception e) {
				JOptionPane.showMessageDialog(SysventoryApp.getMainFrame(), "Datei nicht gefunden, oder konnte nicht gelesen werden");
			}
			if (instance == null) {
				instance = new VisuControll();
				instance.restoreTo(true);
			}
		}
		return instance;
	}

	private VisuControllComputer computer;
	private VisuControllNIC nic;
	private VisuControllOS os;
	private VisuControllPrinter printer;
	private VisuControllProcessor processor;
	private VisuControllSoftware software;

	public VisuControll() {
		this.computer = new VisuControllComputer();
		this.nic = new VisuControllNIC();
		this.os = new VisuControllOS();
		this.printer = new VisuControllPrinter();
		this.processor = new VisuControllProcessor();
		this.software = new VisuControllSoftware();
	}

	public void restoreTo(boolean value) {
		this.computer.restoreTo(value);
		this.nic.restoreTo(value);
		this.os.restoreTo(value);
		this.printer.restoreTo(value);
		this.processor.restoreTo(value);
		this.software.restoreTo(value);
	}

	public VisuControllComputer getComputer() {
		return this.computer;
	}

	public VisuControllNIC getNic() {
		return this.nic;
	}

	public VisuControllOS getOs() {
		return this.os;
	}

	public VisuControllPrinter getPrinter() {
		return this.printer;
	}

	public VisuControllProcessor getProcessor() {
		return this.processor;
	}

	public VisuControllSoftware getSoftware() {
		return this.software;
	}

	public void addDataToRow(List<Computer> data, List<List<List<String>>> list) {

		for (Computer computer : data) {
			List<List<String>> row = new ArrayList<List<String>>();
			List<Computer> singleComputer = new ArrayList<Computer>();
			singleComputer.add(computer);
			this.computer.addDataToRow(singleComputer, row);
			this.nic.addDataToRow(computer.getNics(), row);
			List<OperatingSystem> osList = new ArrayList<OperatingSystem>();
			osList.add(computer.getOs());
			this.os.addDataToRow(osList, row);
			this.printer.addDataToRow(computer.getPrinters(), row);
			List<Processor> processorList = new ArrayList<Processor>();
			processorList.add(computer.getProcessor());
			this.processor.addDataToRow(processorList, row);
			this.software.addDataToRow(computer.getSoftwares(), row);
			list.add(row);

		}
	}

	public void addDataToColumn(List<String> data) {
		this.computer.addDataToColumn(data);
		this.nic.addDataToColumn(data);
		this.os.addDataToColumn(data);
		this.printer.addDataToColumn(data);
		this.processor.addDataToColumn(data);
		this.software.addDataToColumn(data);
	}
}
